package com.ruoyi.pcwl.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.ruoyi.common.utils.StringUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.ruoyi.pcwl.domain.query.ArticleChannelQuery;
import com.ruoyi.pcwl.domain.ArticleChannel;
import com.ruoyi.pcwl.mapper.ArticleChannelMapper;
import com.ruoyi.pcwl.service.ArticleChannelService;


/**
 * 文章分类频道 业务接口实现
 */
@Service("articleChannelService")
public class ArticleChannelServiceImpl extends ServiceImpl<ArticleChannelMapper, ArticleChannel> implements ArticleChannelService {


	/**
	 * 据条件查询记录数
	 */
	@Override
	public Integer findCountByParam(ArticleChannelQuery reqParam) {
		QueryWrapper<ArticleChannel> wrapper = new QueryWrapper<>();
		if (reqParam.getId()!=null)
			wrapper.eq("id",reqParam.getId());
		if (StringUtils.isNotEmpty(reqParam.getChannelName())) {
			wrapper.like("channel_name", reqParam.getChannelName());
		}
		if (reqParam.getChannelNum()!=null)
			wrapper.eq("channel_num",reqParam.getChannelNum());
		if (StringUtils.isNotEmpty(reqParam.getUsername())) {
			wrapper.like("username", reqParam.getUsername());
		}
		Long aLong = baseMapper.selectCount(wrapper);
		return aLong.intValue();
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public IPage<ArticleChannel> selectPage(ArticleChannelQuery reqParam) {
		QueryWrapper<ArticleChannel> wrapper = new QueryWrapper<>();
		if (reqParam.getId()!=null)
			wrapper.eq("id",reqParam.getId());
		if (StringUtils.isNotEmpty(reqParam.getChannelName())) {
			wrapper.like("channel_name", reqParam.getChannelName());
		}
		if (reqParam.getChannelNum()!=null)
			wrapper.eq("channel_num",reqParam.getChannelNum());
		if (StringUtils.isNotEmpty(reqParam.getUsername())) {
			wrapper.like("username", reqParam.getUsername());
		}
		if (MapUtils.isNotEmpty(reqParam.getParams())){
			Map<String, Object> params = reqParam.getParams();
			if (params.keySet().contains("beginTime"))
				wrapper.ge("create_date", params.get("beginTime"));
			if (params.keySet().contains("endTime"))
				wrapper.le("create_date", params.get("endTime"));
		}
		return baseMapper.selectPage(reqParam.getPage(), wrapper);
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(ArticleChannel bean) {
		bean.setCreateDate(new Date());
		return baseMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<ArticleChannel> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return baseMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
//	@Override
//	public Integer addOrUpdateBatch(List<ArticleChannel> listBean) {
//		if (listBean == null || listBean.isEmpty()) {
//			return 0;
//		}
//		return baseMapper.insertOrUpdateBatch(listBean);
//	}

	/**
	 * 多条件更新
	 */
//	@Override
//	public Integer updateByParam(ArticleChannel bean, ArticleChannelQuery param) {
//		return baseMapper.updateByParam(bean, param);
//	}

	/**
	 * 多条件删除
	 */
//	@Override
//	public Integer deleteByParam(ArticleChannelQuery param) {
//		return baseMapper.deleteByParam(param);
//	}

	/**
	 * 根据Id获取对象
	 */
	@Override
	public ArticleChannel getArticleChannelById(Long id) {
		return baseMapper.selectById(id);
	}

	/**
	 * 根据Id修改
	 */
	@Override
	public Integer updateArticleChannelById(ArticleChannel bean) {
		bean.setChangeDate(new Date());
		return baseMapper.updateById(bean);
	}

	/**
	 * 根据Id删除
	 */
	@Override
	public Integer deleteArticleChannelById(Long id) {
		return baseMapper.deleteById(id);
	}

	/**
	 * 取最大编号
	 * @return
	 */
	@Override
	public Integer getMaxNo() {
		QueryWrapper<ArticleChannel> queryWrapper = new QueryWrapper<>();
		queryWrapper.select("IFNULL(Max(channel_num),0) as maxNo");
		List<Map<String, Object>> maps = baseMapper.selectMaps(queryWrapper);
//		Map<String, Object> map = baseMapper.map(queryWrapper);
//		for (Map<String, Object> map : maps) {
//
//			System.out.println(map.get("maxNo"));
//		}
		Integer maxNo = new Integer(maps.get(0).get("maxNo").toString());


		return maxNo;

	}
}